# -*- coding:utf-8 –*-
"""
模块-大众 : TODO
@Author          :王海波
@Version         ：V 1.0.0
@CreateTime       ：2021-02-02 13:20
-----------------------------------
@ModifyTime       : 修改时间
@ModifyContent    : 修改内容
@Remark           : 备注

"""

# import导入模块
import requests
import time
import json
import pymongo
from deDES3 import decrypt_js
from lxml import etree

def send_res(url):
    response = requests.post(url,headers=headers,data=data)
    # print(response.json())
    return response.json()

def main(url):
    result = send_res(url)
    # 第一个参数 加密文本  第二个参数 token
    origin_data = decrypt_js(result['result'], result['secretKey'])
    # print(origin_data)
    get_result(origin_data)



def get_result(data):
    """列表页"""

    resultList = data['queryResult']['resultList']
    for res in resultList:
        print('----------')
        print(res)
        rowkey = res['rowkey']
        time.sleep(4)
        # print(rowkey)
        get_index(rowkey)



def get_index(rowkey):
    try:
        in_headers = {
            'Cookie':'SESSION=d63fc8bb-8f89-4dd7-ae25-6fff225ef242',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        }
        in_data = {
            'docId': rowkey,
            'ciphertext': '1110001 1111000 1000111 1110010 1010010 111001 1000011 110011 1110101 1101011 1110111 1111001 1100011 1110111 1101000 1000101 1000100 1000010 1101000 1011000 1001010 1111000 1101001 1010101 110010 110000 110010 110001 110000 110010 110010 110000 1101000 111000 1000110 1100110 1111000 1010010 1100111 1001001 1101110 1110011 1101100 1100010 1110101 1011000 1000001 1110000 1110100 1000110 1001000 111000 111000 1010001 111101 111101',
            'cfg': 'com.lawyee.judge.dc.parse.dto.SearchDataDsoDTO@docInfoSearch',
            '__RequestVerificationToken': 'O3C4Dj9vxcYLnPJsjCj4wsiv'
        }
        in_url = 'https://wenshu.court.gov.cn/website/parse/rest.q4w'
        response = requests.post(in_url, headers=in_headers, data=in_data).json()
        origin_data = decrypt_js(response['result'], response['secretKey'])
        # print(origin_data)
        save(origin_data)
        pass
    except:
        pass

def save(data):
    re_item = {}
    title = data['s1']  # 标题
    fayuan = data['s2']  # 法院
    anhao = data['s7']   # 案号

    qwContent = data['qwContent']
    html = etree.HTML(qwContent)
    count = []
    count_list = html.xpath('//div')  # 内容
    for i in count_list:
        re = i.xpath('.//text()')[0]
        count.append(re)
    count_re = ''.join(count)
    # print(count_re)
    byrw = ''
    if data['s26']:
        byrw = data['s26']  # 本院认为
    cjue = ''
    if data['s27']:
        cjue = data['s27']  # 裁决
    time_res = ''
    if data['s28']:
        time_res = data['s28']  # 时间
    re_item['title'] = title
    re_item['fayuan'] = fayuan
    re_item['anhao'] = anhao

    re_item['count'] = count_re
    re_item['byrw'] = byrw
    re_item['cjue'] = cjue
    re_item['time_res'] = time_res
    # col.insert(dict(re_item))
    print(title, fayuan, anhao, byrw, cjue, time_res)

# 程序的入口
if __name__ == "__main__":
    # 存储mangodb数据库
    Client = pymongo.MongoClient(host='localhost', port=27017)
    # 进入数据库
    db = Client['caipan']
    # 进入集合
    col = db['caipan_book']

    headers = {
        # 'Cookie': 'SESSION=d63fc8bb-8f89-4dd7-ae25-6fff225ef242',
        # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
        # 'X-Requested-With': 'XMLHttpRequest',
        # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        # 'Referer':'https://wenshu.court.gov.cn/website/wenshu/181217BMTKHNT2W0/index.html?pageId=01c2afbd96a91497ab97b0e56225f105&s21=%E5%80%9F%E6%88%B7',
        # 'Origin':'https://wenshu.court.gov.cn',
        # 'HOST':'wenshu.court.gov.cn'

        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': '978',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'SESSION=d63fc8bb-8f89-4dd7-ae25-6fff225ef242',
        'Host': 'wenshu.court.gov.cn',
        'Origin': 'https://wenshu.court.gov.cn',
        'Referer': 'https://wenshu.court.gov.cn/website/wenshu/181217BMTKHNT2W0/index.html?pageId=01c2afbd96a91497ab97b0e56225f105&s21=%E5%80%9F%E6%88%B7',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    for i in range(1, 41):
        # time.sleep(2)
        data = {
            'pageId': '01c2afbd96a91497ab97b0e56225f105',
            's21': '借户',
            'sortFields': 's50:desc',
            'ciphertext': '111000 110001 111001 1110111 110100 1000001 1001000 1101010 1100110 1111001 1001001 1000111 1001101 1100101 1110001 1101000 1000011 1001001 1100101 1010100 1010001 1100100 1010000 110101 110010 110000 110010 110001 110000 110010 110010 110000 1110100 1001110 1101010 101011 1001100 1101001 1101111 1001001 1001001 1100010 1100111 110111 1101101 1110111 1100110 1011001 1110001 1110010 1000010 110100 1001010 1110111 111101 111101',
            'pageNum': 1,
            # 'pageSize': '15',
            'queryCondition': '[{"key":"s21","value":"借户"},{"key":"s4","value":"3"},{"key":"s8","value":"03"},{"key":"s6","value":"01"},{"key":"cprq","value":"2016-12-01 TO 2021-02-20"}]',
            'cfg': 'com.lawyee.judge.dc.parse.dto.SearchDataDsoDTO@queryDoc',
            '__RequestVerificationToken': 'lhmcHbQcRyByDkImnIOIaiFu'
        }
        # data = json.dumps(data)

        index_url = 'https://wenshu.court.gov.cn/website/parse/rest.q4w'

        result = main(index_url)

        Client.close()
        break
        # 512
